import 'PackageInterface.dart';
import 'SendInterface.dart';

class SendBean {
  //目标蓝牙对象
  String address = "";

  //待发送的数据
  final List<PackageInterface> _packages = [];

  //发送的索引
  int index = -1;

  //是否有响应数据
  bool hasResponse = true;

  /// @param onSuccess 成功回调
  SendCallback? onSuccess;

  /// @param onProgress 进度回调
  SendProgress? onProgress;

  /// @param onError 错误回调
  SendCallback? onError;

  //获取数据包数量
  int getPackageCount() {
    return _packages.length;
  }

  //添加数据包
  void addPackage(PackageInterface package) {
    _packages.add(package);
  }

  //添加数据包组
  void addPackages(List<PackageInterface> packages) {
    _packages.addAll(packages);
  }

  //清空数据包组
  void clear() {
    _packages.clear();
  }

  //发送失败
  void onSendError() {
    index = getPackageCount();
  }

  //是否还有下一条数据包
  bool hasNext() {
    return getNextPackage() != null;
  }

  ///获取下一个要发送的数据包
  PackageInterface? getNextPackage({int? index}) {
    index ??= this.index;
    if (index >= getPackageCount()) {
      return null;
    }
    return _packages[index];
  }
}
